package org.pahema.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pahema.services.ComprobanteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {
	
	private final Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
	private ComprobanteService comprobanteService;
	
	@RequestMapping(value="/home.htm")
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        logger.info("--- Retornando la vista default ---");

        return new ModelAndView("default");
    }
	
    
	@RequestMapping(value = "procesarArchivo", method = RequestMethod.POST)
    public @ResponseBody
    String uploadFileHandler(@RequestParam("file") MultipartFile file) {
 
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
 
                // Creating the directory to store file
                //String rootPath = System.getProperty("catalina.home");
                String rootPath = "C:/";
                File dir = new File(rootPath + File.separator + "tmpFiles");
                
                logger.debug("path: "+dir);
                
                if (!dir.exists()){
                    dir.mkdirs();
                }
                
                // Create the file on server
                File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
                stream.write(bytes);
                stream.close();
 
                logger.info("Server File Location = "+ serverFile.getAbsolutePath());
 
                return "You successfully uploaded file = " + file.getOriginalFilename();
            } catch (Exception e) {
                return "You failed to upload " + file.getOriginalFilename() + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + file.getOriginalFilename() + " because the file was empty.";
        }
    }
	
	@RequestMapping("/listaComprobantes.htm")  
    public ModelAndView getUserList() {  
  
        Map<String, Object> model = new HashMap<String, Object>();  
        model.put("lalala", null);  
        return new ModelAndView("listaComprobantes", model);  
  
    }


	/*--- getters y setters ---*/
	public ComprobanteService getComprobanteService() {
		return comprobanteService;
	}
	
	
	public void setComprobanteService(ComprobanteService comprobanteService) {
		this.comprobanteService = comprobanteService;
	}

}
